// components/login.js
const app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    state: {
      type: Boolean
    } 
  },

  /**
   * 组件的初始数据
   */

  data: {
    show: false,
  },

    /**
   * 组件的生命周期
   */

  lifetimes: {
    attached: function() {

    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    bindGetUserInfo(e) {
      console.log(e.detail.userInfo)
    },
    getpopupstate: function () {
      if (this.data.state == true) {
        this.setData({
          show: false
        })
      } else {
        this.setData({
          show: true
        })
      }
    },
    getPhoneNumber(e) {
      console.log('getPhoneNumber',e)
      var _this = this;
      // if (e) {
      //   this.setData({ dialogShow: false })
      // }
      // console.log(e.detail.errMsg)
      // console.log(e.detail.iv)
      // console.log(e.detail.encryptedData)
      // console.log("e.detail.errMsg:"+e.detail.errMsg)
      wx.login({
        success: res => {
          console.log(e.detail.errMsg)
          if (e.detail.errMsg == 'getPhoneNumber:fail:user deny' || e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
            console.log('拒绝')
            console.log(e.detail.errMsg == 'getPhoneNumber:fail:user deny')
            this.setData({
              show: false
            })
          } else if(e.detail.errMsg == 'getPhoneNumber:ok') { //授权通过执行跳转
            console.log('允许')
            console.log(e.detail.errMsg == 'getPhoneNumber:ok')
            wx.request({
              url: app.globalData.baseUrl + 'auth/jwt/session-key?code=' + res.code+'&museumCode='+app.globalData.museumCode,
              header: {
                'content-type': 'application/json'
              },
              success(res) {
                if (res && res.header && res.header['Set-Cookie']) {
                  console.log('保存cookie',res)
                  wx.setStorageSync('cookieKey', res.header['Set-Cookie']); //保存Cookie到Storage
                }
                if(res.data.rel){
                    wx.setStorageSync('openId', res.data.data)
                    _this.audioGuideLogin(e.detail.encryptedData, e.detail.iv)
                }
              }
            })
          }
        }
      });

    },
    audioGuideLogin:function(enc, iv){
      let that=this
    wx.request({
        url: app.globalData.baseUrl + 'auth/jwt/wechat-token',
        data: {
          "encryptedData": encodeURI(enc),
          "iv": encodeURI(iv),
          "openId":wx.getStorageSync('openId')
        },
        header: {
          'content-type': 'application/json',
          // 'Cookie': wx.getStorageSync('cookieKey')
          'Authorization': wx.getStorageSync('cookieKey')
        },
        success(res) {
          console.log(22222,res)
          if (res.data.data != '获取失败！') {
            wx.setStorageSync('yytoken', res.data.data); //保存语音导览token到Storage
            wx.showToast({
              title: '登录成功',
              icon: 'success',
              duration: 1000
            })
          } else {
            wx.showToast({
              title: '登录失败，请退出重试哦',
              icon: 'none',
              duration: 1000
            })
          }
        }
      })
    },
  }
})
